package com.wk.springbootlearn.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wk.springbootlearn.common.utils.OkHttpCli;
import com.wk.springbootlearn.common.utils.RedisUtils;
import com.wk.springbootlearn.mapper.UserMapper;
import com.wk.springbootlearn.pojo.constant.CodeConstant;
import com.wk.springbootlearn.pojo.entity.User;
import com.wk.springbootlearn.service.TestService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private OkHttpCli okHttpCli;

    @Override
    public User redisTest() {
        User user = userMapper.selectById(3);
        redisUtils.set("user", user);
        return (User) redisUtils.get("user");
    }

    @Override
    public Object httpClientTest() {
        String url = "https://hll-up-dev.herinapp.com/admin/orgs";
        HashMap<String, String> reqHeaders = new HashMap<>();
        reqHeaders.put("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MTE1MDNlMDE0ZThjZDU2NTgzMGQxMDgiLCJjb2RlIjoic3VwZXJoZXJpbiIsIm5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJhdmF0YXIiOiIiLCJqb2IiOiIiLCJnZW5kZXIiOiIiLCJwaG9uZSI6IiIsInJvbGUiOltdLCJvcmdhbml6YXRpb24iOiIiLCJjcmVhdG9yX2lkIjoiIiwiY3JlYXRvcl9jb2RlIjoiIiwiYWRtaW5faXNfc3VwZXIiOnRydWUsInJvb20iOiJiYzUwMWVkM2M2MzQ0MzYwODRiZGNlYTFkMWNkZTcyZSIsInBlcm1pc3Npb24iOlsi5pm65oWn5Zut5Yy6SU9D6L-Q6JCl5Lit5b-DIiwi5pm65oWn5Zut5Yy6SU9D6L-Q6JCl5Lit5b-DLeeZu-W9lSIsIuaZuuaFp-WbreWMuklPQ-i_kOiQpeS4reW_gy3mmbrmhaflronpmL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uaFp-WbreWMuuWQjuWPsOeuoeeQhuezu-e7ny3ns7vnu5_orr7nva4t5Liq5Lq65Lit5b-DIiwi57qi5p-z5p6X5pm65oWn5Zut5Yy65ZCO5Y-w566h55CG57O757ufLeezu-e7n-iuvue9ri3kv67mlLnlr4bnoIEiLCLnuqLmn7Pmnpfmmbrmhaflm63ljLrlkI7lj7DnrqHnkIbns7vnu58t55m75b2VIl0sImlhdCI6MTYzMTA4MzEyNywiZXhwIjoxNjMxMzc1OTk5fQ.zl9Ix8FYWfHdBFIPRwlnhQfCx0ELjft2eDc7KVheX2s");
        String mes = okHttpCli.doPost(url, null, reqHeaders);
        if (mes == null) return null;
        JSONObject jsonObject = JSON.parseObject(mes);
        if (jsonObject.get("code") != null && (int) jsonObject.get("code") == CodeConstant.SUCCESS) {
            return jsonObject.get("data");
        }else {
            return mes;
        }
    }
}
